# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 守护进程.py
Author: kij
Create Date: 2021/3/13
-------------------------------------------------
"""
from multiprocessing import Process
from threading import currentThread
import os
import time


class MyProcess(Process):
	def __init__(self, name):
		super().__init__()
		self.name = name

	def run(self):
		print('进程%s' % self.is_alive())
		print("开始进程")
		time.sleep(3)
		print('%s 进程结束' % self.name)


if __name__ == "__main__":
	p = MyProcess("子进程")
	# 守护进程，主进程结束后子进程结束 必须在start之前设置
	p.daemon = True
	p.start()
	time.sleep(1)
	p.terminate()
	print('进程%s' % p.is_alive())
	# join 子进程结束后主进程再结束
	p.join()
	print('进程%s' % p.is_alive())
	print('主进程pid=%s ppid=%s' % (os.getpid(), os.getppid()))
	print('线程%s' % currentThread().getName())
